﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_SeaGateCompConP : DockContent
    {
        private DGBY.BLL.BGHConPeoTelTab m_bllBGHConPeoTelTab = new DGBY.BLL.BGHConPeoTelTab();
        private DGBY.BLL.BGHDataTab m_bllBGHDataTab = new DGBY.BLL.BGHDataTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectBGHConPeoTelTabID = 0;
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];


        public Frm_SeaGateCompConP()
        {
            InitializeComponent();
        }

        private void Frm_SeaGateCompConP_Load(object sender, EventArgs e)
        {
            DataSet ds = m_bllBGHDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxChooseSeaGateComp.DataSource = ds.Tables[0];
                this.comboBoxChooseSeaGateComp.DisplayMember = "报关行名称";
                this.comboBoxChooseSeaGateComp.ValueMember = "BGHDataTabID";
                this.comboBoxChooseSeaGateComp.SelectedIndex = 0;
            }
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDel.Visible = true;
            }
        }

        private void comboBoxChooseSeaGateComp_SelectedIndexChanged(object sender, EventArgs e)
        {
            //try
            //{
            //    string strCompID = this.comboBoxChooseSeaGateComp.SelectedValue.ToString();
            //    dataGridViewSeaGateCompPersonAndPhoneList.DataSource = null;
            //    m_ds = m_bllBGHConPeoTelTab.GetConPListByCompID(strCompID);
            //    if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            //    {
            //        dataGridViewSeaGateCompPersonAndPhoneList.DataSource = m_ds.Tables[0];
            //        dataGridViewSeaGateCompPersonAndPhoneList.Columns[0].Visible = false;
            //    }
            //}
            //catch { }
        }

        private void ShowAllRecordInDataGridView()
        {
            dataGridViewSeaGateCompPersonAndPhoneList.DataSource = null;
            m_ds = m_bllBGHConPeoTelTab.GetAllConPList();
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewSeaGateCompPersonAndPhoneList.DataSource = m_ds.Tables[0];
                dataGridViewSeaGateCompPersonAndPhoneList.Columns[0].Visible = false;
            }
        }

        private void buttonShowAllRecord_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void dataGridViewSeaGateCompPersonAndPhoneList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strBGHConPeoTelTabID = this.dataGridViewSeaGateCompPersonAndPhoneList.Rows[this.dataGridViewSeaGateCompPersonAndPhoneList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectBGHConPeoTelTabID = Convert.ToInt32(strBGHConPeoTelTabID, 10);
            cellclickBGHConPeoTelTab();
        }

        private void cellclickBGHConPeoTelTab()
        {
            DGBY.Model.BGHConPeoTelTab modelBGHConPeoTelTab = m_bllBGHConPeoTelTab.GetModel(m_nSelectBGHConPeoTelTabID);
            if (modelBGHConPeoTelTab != null)
            {
                if (modelBGHConPeoTelTab.BGHDataTabID != null)
                    this.comboBoxChooseSeaGateComp.SelectedValue = modelBGHConPeoTelTab.BGHDataTabID;
                this.textBoxConPerson.Text = modelBGHConPeoTelTab.BGHConPeoName;
                this.textBoxConPhone.Text = modelBGHConPeoTelTab.BGHConPeoTel;
            }
            else
            {
                this.textBoxConPerson.Text = "";
                this.textBoxConPhone.Text = "";
            }
        }

        private void dataGridViewSeaGateCompPersonAndPhoneList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewSeaGateCompPersonAndPhoneList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.BGHConPeoTelTab modelBGHConPeoTelTab = new DGBY.Model.BGHConPeoTelTab();
                modelBGHConPeoTelTab.BGHConPeoName = this.textBoxConPerson.Text;
                modelBGHConPeoTelTab.BGHConPeoTel = this.textBoxConPhone.Text;

                if (this.comboBoxChooseSeaGateComp.SelectedValue != null)
                    modelBGHConPeoTelTab.BGHDataTabID = Convert.ToInt32(this.comboBoxChooseSeaGateComp.SelectedValue.ToString(), 10);

                int nResult = m_bllBGHConPeoTelTab.Add(modelBGHConPeoTelTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加", this.comboBoxChooseSeaGateComp.Text + "(" + modelBGHConPeoTelTab.BGHConPeoName + ")");

                    m_nSelectBGHConPeoTelTabID = m_bllBGHConPeoTelTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllBGHConPeoTelTab.GetModel(m_nSelectBGHConPeoTelTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                DGBY.Model.BGHConPeoTelTab modelBGHConPeoTelTab = m_bllBGHConPeoTelTab.GetModel(m_nSelectBGHConPeoTelTabID);
                modelBGHConPeoTelTab.BGHConPeoName = this.textBoxConPerson.Text;
                modelBGHConPeoTelTab.BGHConPeoTel = this.textBoxConPhone.Text;
                //modelBGHConPeoTelTab.BGHConPeoTelTabID = m_nSelectBGHConPeoTelTabID;

                if (this.comboBoxChooseSeaGateComp.SelectedValue != null)
                    modelBGHConPeoTelTab.BGHDataTabID = Convert.ToInt32(this.comboBoxChooseSeaGateComp.SelectedValue.ToString(), 10);

                m_bllBGHConPeoTelTab.Update(modelBGHConPeoTelTab);

                Frm_Main.Log2DB(this.Text, "修改", this.comboBoxChooseSeaGateComp.Text + "(" + modelBGHConPeoTelTab.BGHConPeoName + ")");

                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
         
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (m_bllBGHConPeoTelTab.GetModel(m_nSelectBGHConPeoTelTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllBGHConPeoTelTab.Delete(m_nSelectBGHConPeoTelTabID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclickBGHConPeoTelTab();
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dataGridViewSeaGateCompPersonAndPhoneList.DataSource;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "BGHConP.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.DataTableToExcel(dt, 30, 6, 3, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("数据不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}